﻿using System;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using ConfExpert.Extension;
using ConfExpert.Forms;

namespace ConfExpert.Utility
{
    [Serializable]
    public class AppSettings
    {
        public bool EnableAppearanceEvenRow = true;
        public DefaultBoolean AllowFormGlass = DefaultBoolean.False;
        public PopupFilterMode PopupFilterMode = PopupFilterMode.Contains;
        public EditorShowMode EditorShowMode = EditorShowMode.MouseDownFocused;

        public string ZorunluAlanRengi = "Gold";
        public string FormAciklamaRengi = "Black";
        public string SkinName = Login.SkinName;

        public string StartupForm = null;
        public string StartupRibbonPage = null;

        private void SetEditorMode(Control Page)
        {
            foreach (Control ctrl in Page.Controls)
                if (ctrl is GridControl)
                {
                    if ((ctrl as GridControl).MainView is GridView)
                        ((ctrl as GridControl).MainView as GridView).OptionsBehavior.EditorShowMode = EditorShowMode;
                }
                else if (ctrl.Controls.Count > 0)
                    SetEditorMode(ctrl);
        }

        private void SetEvenRow(Control Page)
        {
            foreach (Control ctrl in Page.Controls)
                if (ctrl is GridControl)
                {
                    if ((ctrl as GridControl).MainView is GridView)
                        ((ctrl as GridControl).MainView as GridView).OptionsView.EnableAppearanceEvenRow = EnableAppearanceEvenRow;
                }
                else if (ctrl.Controls.Count > 0)
                    SetEvenRow(ctrl);
        }

        public void Apply()
        {
            AppSettings settings = new AppSettings();

            settings.StartupForm = this.StartupForm;
            settings.StartupRibbonPage = this.StartupRibbonPage;
            settings.PopupFilterMode = this.PopupFilterMode;

            foreach (int rpval in Enum.GetValues(typeof(Rp)))
                if (RepositoryManager.Get(rpval) is RepositoryItemGridLookUpEdit)
                    (RepositoryManager.Get(rpval) as RepositoryItemGridLookUpEdit).PopupFilterMode = PopupFilterMode;
                else
                    (RepositoryManager.Get(rpval) as RepositoryItemSearchLookUpEdit).PopupFilterMode = PopupFilterMode;
                
            settings.EditorShowMode = this.EditorShowMode;
            foreach (XtraForm form in AppSession.MainForm.MdiChildren)
                    SetEditorMode(form);

            settings.EnableAppearanceEvenRow = this.EnableAppearanceEvenRow;
            foreach (XtraForm form in AppSession.MainForm.MdiChildren)
                    SetEvenRow(form);

            settings.ZorunluAlanRengi = this.ZorunluAlanRengi;
            settings.FormAciklamaRengi = this.FormAciklamaRengi;

            AppSession.MainForm.AllowFormGlass = this.AllowFormGlass;
            settings.AllowFormGlass = this.AllowFormGlass;
        }

        public static AppSettings Load(string xmlSettings)
        {
            if (xmlSettings.Trim() == string.Empty)
                return new AppSettings();
            xmlSettings = xmlSettings.Replace("PrgSettings", "AppSettings");
            AppSettings settings = XmlExtensions.XmlDeserializeFromString<AppSettings>(xmlSettings);
            return settings;
        }
    }
}